//
//  ViewController.swift
//  progressview
//
//  Created by ap0stader on 2023/10/25.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    var timer : Timer!
    
    @IBOutlet var theActivityIndicator: UIActivityIndicatorView!
    
    @IBOutlet var theProgress: UIProgressView!
    
    @IBOutlet var theStartButton: UIButton!
    
    @IBOutlet var theClearButton: UIButton!
    
    @IBAction func startButton(_ sender: Any) {
        if theProgress.progress == 0 {
            theActivityIndicator.startAnimating()
            
            timer = Timer.scheduledTimer(timeInterval: 0.05, target: self, selector: #selector(ViewController.going), userInfo: nil, repeats: true)
            
            theStartButton.isEnabled = false
        }
    }
    
    @objc func going() {
        theProgress.progress = theProgress.progress + 0.01
        
        if (theProgress.progress == 1.0) {
            timer.invalidate()
            
            theActivityIndicator.stopAnimating()
            
            theStartButton.isEnabled = false
            
            theClearButton.isEnabled = true
        }
    }
    
    @IBAction func clearButton(_ sender: Any) {
        theProgress.progress = 0
        
        theStartButton.isEnabled = true
        
        theClearButton.isEnabled = false
    }
}

